/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package plash.components;

import plash.display.Graphics;
import plash.display.Sprite;
import plash.events.Event;
import plash.events.IEventListener;
import plash.events.MouseEvent;
import plash.text.TextField;

/**
 *
 * @author #0K Srinivasan <hashok.com>
 */
public class ListBox extends Sprite implements IEventListener
{
    private int _listWidth = 100;
    private int _itemHoverColor = 0xFFFFFF;
    private int _itemNormalColor = 0xFFFFFF;
    private int _itemClickColor = 0xFFFFFF;
    
    private int _currentIndex = -1;
    private String _currentValue = "";
    
    private String[] _listItemValues;
    private ListItem[] _listItemsUI;
    private Sprite _itemsHolder = new Sprite();
    private Sprite _listBackground = new Sprite();

    public ListBox()
    {
        super();
        _initialize();
    }

    public ListBox(String[] items)
    {
        this();
        
        if (items.length > 0)
        {
            for (int i = 0; i < items.length; i++)
            {
                this.addItem(items[i]);
            }
        }
        _invalidate();
    }

    private void _initialize()
    {
        _listItemValues = new String[0];
        _listItemsUI = new ListItem[0];

        this.addChild(_itemsHolder);
        this.addChild(_listBackground);
    }

    private void _invalidate()
    {
        for (int i = 0; i < _listItemsUI.length; i++)
        {
            _listItemsUI[i].setY(_listItemsUI[i].getItemHeight() * i);
            _listItemsUI[i].setItemWidth(this._listWidth);
            _listItemsUI[i].setItemHeight(20);
            _listItemsUI[i].setItemClickColor(this._itemClickColor);
            _listItemsUI[i].setItemHoverColor(this._itemHoverColor);
            _listItemsUI[i].setItemNormalColor(this._itemNormalColor);
        }
    }

    public void addItem(String value)
    {
        String[] tempItemValues;
        ListItem[] tempItemsUI;
        ListItem item;

        if (_listItemValues.length == 0)
        {
            _listItemValues = new String[1];
            _listItemsUI = new ListItem[1];
            _currentIndex = 0;
            _currentValue = value;
        }
        else
        {
            tempItemValues = new String[_listItemValues.length];
            tempItemsUI = new ListItem[_listItemsUI.length];

            System.arraycopy(_listItemValues, 0, tempItemValues, 0, _listItemValues.length);
            System.arraycopy(_listItemsUI, 0, tempItemsUI, 0, _listItemsUI.length);

            _listItemValues = new String[_listItemValues.length + 1];
            _listItemsUI = new ListItem[_listItemsUI.length + 1];

            System.arraycopy(tempItemValues, 0, _listItemValues, 0, tempItemValues.length);
            System.arraycopy(tempItemsUI, 0, _listItemsUI, 0, tempItemsUI.length);
        }
        item = new ListItem(value, _listItemValues.length - 1);

        _listItemValues[_listItemValues.length - 1] = value;
        _listItemsUI[_listItemsUI.length - 1] = item;

        item.addEventListener("onSelect", this);
        _itemsHolder.addChild(item);
        _invalidate();
    }

    public int getListWidth()
    {
        return _listWidth;
    }

    public void setListWidth(int listWidth)
    {
        this._listWidth = listWidth;
        _invalidate();
    }

    public int getItemHoverColor()
    {
        return _itemHoverColor;
    }

    public void setItemHoverColor(int itemHoverColor)
    {
        this._itemHoverColor = itemHoverColor;
        _invalidate();
    }

    public int getItemNormalColor()
    {
        return _itemNormalColor;
    }

    public void setItemNormalColor(int itemNormalColor)
    {
        this._itemNormalColor = itemNormalColor;
        _invalidate();
    }

    public int getItemClickColor()
    {
        return _itemClickColor;
    }

    public void setItemClickColor(int itemClickColor)
    {
        this._itemClickColor = itemClickColor;
        _invalidate();
    }
    
    public String getValue()
    {
        return _currentValue;
    }
    
    public int getIndex()
    {
        return _currentIndex;
    }
    
    public void setIndex(int index)
    {
        if(index < (this._listItemValues.length - 1))
        {
            _currentIndex = index;
            _currentValue = _listItemValues[index];
        }
    }
    
    public int getTotal()
    {
        return _listItemValues.length;
    }

    @Override
    public void event(Event e)
    {
        if(e.target instanceof ListItem)
        {
            if(e.name.equals("onSelect"))
            {
                _currentIndex = ((ListItem)(e.target)).getIndex();
                _currentValue = ((ListItem)(e.target)).getValue();
                
                dispatchEvent(new Event("onItemSelect"));
            }
        }
    }

    /**
     * Start of the ListItem Class. This class is a private class and is not
     * accessible outside the ListClass
     */
    class ListItem extends Sprite implements IEventListener
    {

        private int _itemWidth = 50;
        private int _itemHeight = 50;
        private int _index = -1;
        private int _itemHoverColor = 0xFFFFFF;
        private int _itemNormalColor = 0xFFFFFF;
        private int _itemClickColor = 0xFFFFFF;
        private String _value = "";
        private TextField _field = new TextField();
        private Sprite _listItemBackground = new Sprite();

        public ListItem(String value, int index)
        {
            super();
            _index = index;
            _value = value;
            _initialize();
        }

        private void _initialize()
        {
            _listItemBackground.addEventListener(MouseEvent.MOUSE_OVER, this);
            _listItemBackground.addEventListener(MouseEvent.MOUSE_OUT, this);
            _listItemBackground.addEventListener(MouseEvent.MOUSE_DOWN, this);
            _listItemBackground.addEventListener(MouseEvent.MOUSE_UP, this);

            this.addChild(_listItemBackground);
            this.addChild(_field);

            _setRollOutState();
            _invalidate();
        }

        private void _invalidate()
        {
            _field.setText(_value);
            _field.setWidth(_itemWidth);
            _field.setHeight(_itemHeight);
            _setRollOutState();
        }

        private void _setRollOverState()
        {
            Graphics g = _listItemBackground.getGraphics();
            g.clear();
            g.setLineStyle(1, 0x000000, 1.0f);
            g.beginFill(this._itemHoverColor, 1.0f);
            g.drawRectangle(0, 0, this._itemWidth, this._itemHeight);
            g.endFill();
        }

        private void _setRollOutState()
        {
            Graphics g = _listItemBackground.getGraphics();
            g.clear();
            g.setLineStyle(1, 0x000000, 1.0f);
            g.beginFill(this._itemNormalColor, 1.0f);
            g.drawRectangle(0, 0, this._itemWidth, this._itemHeight);
            g.endFill();
        }

        private void _setMouseDownState()
        {
            Graphics g = _listItemBackground.getGraphics();
            g.clear();
            g.setLineStyle(1, 0x000000, 1.0f);
            g.beginFill(this._itemClickColor, 1.0f);
            g.drawRectangle(0, 0, this._itemWidth, this._itemHeight);
            g.endFill();
        }

        @Override
        public void event(Event e)
        {
            if (e.target == _listItemBackground)
            {
                if (e.name.equals(MouseEvent.MOUSE_OVER))
                {
                    _setRollOverState();
                }
                else if (e.name.equals(MouseEvent.MOUSE_OUT))
                {
                    _setRollOutState();
                }
                else if (e.name.equals(MouseEvent.MOUSE_DOWN))
                {
                    _setMouseDownState();
                    dispatchEvent(new Event("onSelect"));
                }
                else if (e.name.equals(MouseEvent.MOUSE_UP))
                {
                    _setRollOutState();
                }
            }
        }

        public void setSize(int width, int height)
        {
            this._itemWidth = width;
            this._itemHeight = height;
            _invalidate();
        }

        public int getIndex()
        {
            return _index;
        }

        public int getItemWidth()
        {
            return _itemWidth;
        }

        public void setItemWidth(int itemWidth)
        {
            this._itemWidth = itemWidth;
            _invalidate();
        }

        public int getItemHeight()
        {
            return _itemHeight;
        }

        public void setItemHeight(int itemHeight)
        {
            this._itemHeight = itemHeight;
            _invalidate();
        }

        public String getValue()
        {
            return _value;
        }

        public void setValue(String value)
        {
            this._value = value;
            _invalidate();
        }

        public int getItemHoverColor()
        {
            return _itemHoverColor;
        }

        public void setItemHoverColor(int itemHoverColor)
        {
            this._itemHoverColor = itemHoverColor;
        }

        public int getItemNormalColor()
        {
            return _itemNormalColor;
        }

        public void setItemNormalColor(int itemNormalColor)
        {
            this._itemNormalColor = itemNormalColor;
        }

        public int getItemClickColor()
        {
            return _itemClickColor;
        }

        public void setItemClickColor(int itemClickColor)
        {
            this._itemClickColor = itemClickColor;
        }
    }
}
